import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)

const state = {
  stacks: {
    words: [],
    grammars: []
  },
  editedSentenceId: {
    videoNum: undefined,
    seq: undefined
  },
  editedWord: {
    content: '',
    pitch: '',
    partOfSpeech: '',
    meaning: '',
    category: 'Select',
    special: false
  },
  editedGrammar: {
    category: '',
    contentList: [{
      seq: '1',
      content: ''
    }]
  }
}
const actions = {
  //调用：this.$store.dispatch('queryData', 111)
  async queryStacks (context, payload) {
    console.log(payload)
    // 调用接口获取数据
    const response = {};
    // context类似于this.$store
    context.commit('updateStacks', response.data.list)
  }
}
const mutations = {
  updateStacks (state, payload) {
    if (payload.word !== undefined && payload.word !== null) {
      state.stacks.words.unshift(payload.word);
    }
    if (payload.grammar !== undefined && payload.grammar !== null) {
      state.stacks.grammars.unshift(payload.grammar);
    }
  },
  updateEditedSentenceId (state, payload) {
    state.editedSentenceId = payload;
  },
  updateEditedWord (state, payload) {
    state.editedWord = payload;
  },
  updateEditedGrammar (state, payload) {
    state.editedGrammar = payload;
  },
  addContentListToGrammar (state, payload) {
    state.editedGrammar.contentList.push(payload);
  }
}
const getters = {
  stacks (state) {
    return state.stacks;
  },
  editedSentenceId(state) {
    return state.editedSentenceId;
  },
  editedWord (state) {
    return state.editedWord;
  },
  editedGrammar (state) {
    return state.editedGrammar;
  }
}
const store = new Vuex.Store({
  state,
  actions,
  mutations,
  getters
});
export default store;